﻿using System.Collections.Generic;
using System.Xml;

namespace MingleReport
{
    public class XmlDocumentParser<T> where T : IXmlNodeParser<T> , new ()
    {
        public List<T> ParseFile(string fileName)
        {
            XmlDataDocument xmlDoc = new XmlDataDocument();
            xmlDoc.Load(fileName);
            return Parse(xmlDoc);
        }

        public virtual List<T> Parse(string xmlString)
        {
            XmlDataDocument xmlDoc = new XmlDataDocument();
            xmlDoc.LoadXml(xmlString);
            return Parse(xmlDoc);
        }

        private List<T> Parse(XmlDataDocument xmlDoc)
        {
            List<T> Ts = new List<T>();
            foreach (XmlNode xmlNode in xmlDoc.ChildNodes[1].ChildNodes)
            {
                Ts.Add(new T().ToObject(xmlNode));
            }
            return Ts;
        }
    }
}